//1. 登录注册切换
window.onload = function () {
    $('.login-box a').on('click', function () {
        $('.login-box').hide()
        $('.res-box').show()
    })
    $('.res-box a').on('click', function () {
        $('.login-box').show()
        $('.res-box').hide()
    })

    // 2.表单验证
    let form = layui.form
    form.verify({
        username: [
            /^[\S]{6,15}$/,
            '用户名只能是6-15位非空字符'
        ],
        password: [
            /^[a-zA-Z0-9]{6,12}$/,
            '密码只能是6-12位英文字母和数字'
        ],
        repassword: function (value) {
            let pwd = $('.res-box [name ="password"]').val()
            if (pwd != value) {
                return '两次输入密码不一致请重新输入'
            }
        }
    })
   
    // 注册功能发送Ajax
    $('.res-box form').on('submit', function (e) {
        e.preventDefault()
        axios({
            url:'/api/reguser',
            method:'POST',
            data:$(this).serialize()
           
        }).then(({ data: res }) => {
            
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('注册账号成功')
            $('.res-box a').click()
            $('.res-box form')[0].reset()
        });
    })

    //登录功能发送Ajax
    $('.login-box form').on('submit', function (e) {
        e.preventDefault()
        axios({
            url:'/api/login',
            method:'POST',
            data:$(this).serialize()
           
        }).then(({ data: res }) => {
            
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('登录成功')
            setTimeout(function () {
                location.href = '/index.html'
            },1000)
            
        });
    })
    
}
